<template>
  <div class="directory-container">
    <span>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleFormRef"
        label-width="80px"
      >
        <el-form-item v-if="!$route.query.id" label="所属学科">
          <el-select
            v-model="ruleForm.subject"
            placeholder="请选择学科"
            style="width:100%"
            @change="onSubjectChange"
          >
            <el-option
              :label="item.label"
              :value="item.value"
              v-for="(item, index) in simpleSubjectList"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="subject">
          <el-input
            v-model="ruleForm.name"
            placeholder="请输入目录名称"
          ></el-input>
        </el-form-item>
      </el-form>
    </span>
    <span slot="footer" class="dialog-footer">
      <div class="right">
        <el-button @click="$emit('close')">取 消</el-button>
        <el-button type="primary" @click="addDirectory">
          确 定
        </el-button>
      </div>
    </span>
  </div>
</template>

<script>
import { simple } from '../../api/hmmm/subjects'
import { add, update } from '../../api/hmmm/directorys'

export default {
  name: 'DirectoryDialog',
  props: {
    editList: {
      type: Object,
      default: () => {}
    },
    title: {
      type: String
    }
  },
  data() {
    return {
      ruleForm: {
        name: '',
        subject: ''
      },
      rules: {
        subject: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      },
      simpleSubjectList: [], // 简单学科列表
      subjectID: '',
      directoryName: ''
    }
  },
  created() {
    this.loadDirectpory()
  },
  methods: {
    async loadDirectpory() {
      try {
        const { data: res } = await simple()
        // console.log(res)
        this.simpleSubjectList = res

        if (this.title === '修改') {
          this.ruleForm.subject = this.editList.subjectID
          this.ruleForm.name = this.editList.directoryName
        }
      } catch (error) {
        this.$message('数据获取失败')
      }
    },
    onSubjectChange(val) {
      if (this.title === '新增') {
        this.subjectID = val
      } else {
        this.ruleForm.subject = val
        this.subjectID = val
      }
    },
    addDirectory() {
      this.$refs.ruleFormRef.validate(async valid => {
        // console.log(this.ruleForm)
        if (!valid) return this.$message('目录名称不能为空')
        try {
          if (this.title === '新增') {
            await add({
              subjectID: this.subjectID,
              directoryName: this.ruleForm.name
            })
            this.$emit('close')
            this.$message.success('添加成功')
          } else {
            await update({
              id: this.editList.id,
              subjectID: this.subjectID,
              directoryName: this.ruleForm.name
            })
            // console.log(res)
            this.$emit('close')
            this.$message.success('修改成功')
          }
        } catch (error) {
          // console.log(error)
          this.$message.error('操作失败')
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.directory-container {
  padding: 0 10px;
  margin: 10px 0;

  .dir-btn-box {
    margin-left: -70px;
  }

  .right {
    text-align: right;
  }

  .dir-table {
    margin: 15px 0;
  }
}
</style>
